﻿using System.Application;
using System.Collections.Generic;
using System.Domain.Entity;
using Microsoft.AspNetCore.Mvc;
using Ubiquity.Core.Extend;
using Ubiquity.Core.Web;
using Ubiquity.Web.Filters;

namespace Ubiquity.Web.Areas.SystemManage.Controllers
{
    /// <summary>
    ///模块权限 控制器 
    /// </summary>	

    [Area("SystemManage")]
    public class SysModuleAuthorizeController : BaseController
    {
        private readonly SysModuleAuthorizeService _service;

        public SysModuleAuthorizeController(SysModuleAuthorizeService service)
        {
            _service = service;
        }

        [HttpGet]
        [Authorize]
        public ActionResult GetGridPaginationJson(WebPagination pagination, string moduleId = "")
        {
            var paging = pagination.Change();
            var data = new
            {
                rows = _service.GetList(paging, moduleId),
                pagination.total,
                page = pagination.page ,
                pagination.records
            };
            return Content(data.ToJson());
        }

        //[HttpGet]
        //[HandlerAjaxOnly]
        //public ActionResult GetGridJson()
        //{
        //    var data = _app.GetList();
        //    return Content(data.ToJson());
        //}
        [HttpGet]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = _service.GetEntity(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Insert(SysModuleAuthorizeEntity entity)
        {
            entity.UrlAddress = entity.UrlAddress.Trim();
            return _service.Insert(entity) ? Success("操作成功。") : Error("操作失败。");
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Update(SysModuleAuthorizeEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Id = keyValue;
                entity.UrlAddress = entity.UrlAddress.Trim();
                return _service.Update(entity) ? Success("操作成功。") : Error("操作失败。");
            }
            return Error("主键验证错误");
        }
        /// <summary>
        /// 快速添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public ActionResult FastForm()
        {
            return View();
        }
        /// <summary>
        /// 快速添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult FastSubmit(string moduleId, string fullName, string address)
        {
            address = address.Trim();
            List<SysModuleAuthorizeEntity> models = new List<SysModuleAuthorizeEntity>()
            {
                new SysModuleAuthorizeEntity
                {
                    EnCode="from",
                    JsEvent ="",
                    ModuleId =moduleId,
                    FullName ="新增编辑界面",
                    UrlAddress =address+"Form",
                    Location =3,
                    EnabledMark =true,
                    SortCode =10,
                    ParentId ="0"
                },
                 new SysModuleAuthorizeEntity {
                     EnCode ="select",
                     JsEvent ="",
                     ModuleId =moduleId,
                     FullName ="查询",
                     UrlAddress =address+"GetGridPaginationJson",
                     Location =3,
                     EnabledMark =true,
                     SortCode =10,ParentId="0"},
                new SysModuleAuthorizeEntity
                {
                    EnCode="NF-add",
                    JsEvent ="btn_add()"
                    ,ModuleId=moduleId,
                    FullName ="新增"+fullName,
                    UrlAddress =address+"Insert",
                    Location =4,
                    EnabledMark =true,
                    SortCode =10,ParentId="0"
                },
                new SysModuleAuthorizeEntity
                {
                    EnCode="NF-edit",
                    JsEvent ="btn_edit()",
                    ModuleId =moduleId,
                    FullName ="编辑"+fullName,
                    UrlAddress =address+"Update",
                    Location =4,
                    EnabledMark =true,
                    SortCode =10,
                    ParentId ="0"
                },
                new SysModuleAuthorizeEntity
                {
                    EnCode ="NF-delete",
                    JsEvent ="btn_delete()",
                    ModuleId =moduleId,
                    FullName ="删除"+fullName,
                    UrlAddress =address+"DeleteForm",
                    Location =4,
                    EnabledMark =true,
                    SortCode =10,
                    ParentId ="0"

                }


            };

            foreach (var item in models)
            {
                _service.Insert(item);
            }

            //entity.UrlAddress = entity.UrlAddress.Trim();
            //return _app.Insert(entity) ? Success("操作成功。") : Error("操作失败。");
            return Success("操作成功。");
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            return _service.Delete(keyValue) ? Success("操作成功。") : Error("操作失败。");
        }
    }

}
